package org.darcy.service.business.impl;

import java.util.List;

import org.darcy.dao.business.BusinessFinancialReceivedMapper;
import org.darcy.entity.business.BusinessFinancialReceived;
import org.darcy.framework.constant.SysConstant;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.service.business.BusinessContractService;
import org.darcy.service.business.BusinessFinancialReceivedService;
import org.darcy.service.sys.SysFileService;
import org.darcy.service.work.WorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * BusinessFinancialReceived服务实现
 */
@Service
@Transactional
public class BusinessFinancialReceivedServiceImpl extends GenericServiceImpl<BusinessFinancialReceived>
		implements BusinessFinancialReceivedService {

	@Autowired
	private BusinessFinancialReceivedMapper mapper;

	@Autowired
	private BusinessContractService BusinessContractService;

	@Autowired
	private WorkService WorkService;

	@Autowired
	@Lazy
	private SysFileService SysFileService;

	/**
	 * 处理前端数据
	 */
	private void processData(BusinessFinancialReceived record) {
		// 处理附件
		if (record.getPayFileList() != null && !record.getPayFileList().isEmpty()) {
			String file = SysFileService.getFileIdList(record.getPayFileList());
			record.setPayFile(file);
		}

	}

	/**
	 * 处理工作流数据：主生成审批任务
	 */
	private void processWork(BusinessFinancialReceived record) {
		WorkService.addWorkTask(record, null, null, SysConstant.FormType.FinancialReceived);
	}

	@Override
	public BusinessFinancialReceived update(BusinessFinancialReceived record) {
		processData(record);
		super.update(record);
		processWork(record);
		return record;
	}

	@Override
	public BusinessFinancialReceived getDetail(Integer id) {
		BusinessFinancialReceived record = mapper.selectById(id);
		record.setContract(BusinessContractService.getDetail(record.getContractId()));
		// 文件处理
		if (StrUtil.isNotEmpty(record.getPayFile())) {
			record.setPayFileList(SysFileService.getFilePathList(record.getPayFile()));
		}
		return record;
	}

	@Override
	public PageData<BusinessFinancialReceived> page(PageParam param) {
		PageData<BusinessFinancialReceived> data = super.getPageData(param);
		return data;
	}

	@Override
	public List<BusinessFinancialReceived> getList(Integer contractId) {
		QueryWrapper<BusinessFinancialReceived> q = new QueryWrapper<BusinessFinancialReceived>();
		q.eq("contract_id", contractId);
		List<BusinessFinancialReceived> list = this.getList(q);
		for (BusinessFinancialReceived a : list) {
			String time = DateUtil.format(a.getPayTime(), DatePattern.PURE_DATE_FORMATTER);
			a.setDetail("收款时间：" + time + ",  收款金额：" + a.getPayAmount());
		}
		return list;
	}

}
